package cc.mrbird.febs.system.controller;

import cc.mrbird.febs.common.annotation.Log;
import cc.mrbird.febs.common.entity.DrawlingTree;
import cc.mrbird.febs.common.utils.FebsUtil;
import cc.mrbird.febs.common.entity.FebsConstant;
import cc.mrbird.febs.common.controller.BaseController;
import cc.mrbird.febs.common.entity.FebsResponse;
import cc.mrbird.febs.common.entity.QueryRequest;
import cc.mrbird.febs.common.exception.FebsException;
import cc.mrbird.febs.system.entity.Drawing;
import cc.mrbird.febs.system.service.IDrawingService;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.wuwenze.poi.ExcelKit;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;
import java.util.Map;

/**
 *  Controller
 *
 * @author MrBird
 * @date 2019-07-10 18:56:56
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/drawing")
public class DrawingController extends BaseController {

    @Autowired
    private IDrawingService drawingService;


    @GetMapping("/drawing")
    @RequiresPermissions("drawing:list")
    public FebsResponse getAllDrawings(Drawing drawing) {
        return new FebsResponse().success().data(drawingService.findDrawings(drawing));
    }

    @Log("分页列表Drawing")
    @GetMapping("/list")
    @RequiresPermissions("drawing:view")
    public FebsResponse drawingList(QueryRequest request, Drawing drawing) {
        Map<String, Object> dataTable = getDataTable(this.drawingService.findDrawings(request, drawing));
        return new FebsResponse().success().data(dataTable);
    }

    @Log("新增Drawing")
    @PostMapping("/add")
    @RequiresPermissions("drawing:add")
    public FebsResponse addDrawing(@Valid Drawing drawing) throws FebsException {
        try {
            this.drawingService.createDrawing(drawing);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "新增Drawing失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    @Log("批量删除")
    @GetMapping("delete/{drawingIds}")
    @RequiresPermissions("drawing:delete")
    public FebsResponse deleteDrawings(@NotBlank(message = "{required}") @PathVariable String drawingIds) throws FebsException {
        try {
            String[] ids = drawingIds.split(StringPool.COMMA);
            this.drawingService.batchDelete(ids);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "删除失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    @Log("删除Drawing")
    @GetMapping("/delete")
    @RequiresPermissions("drawing:delete")
    public FebsResponse deleteDrawing(Drawing drawing) throws FebsException {
        try {
            this.drawingService.deleteDrawing(drawing);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "删除Drawing失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    @Log("修改Drawing")
    @PostMapping("/update")
    @RequiresPermissions("drawing:update")
    public FebsResponse updateDrawing(Drawing drawing) throws FebsException {
        try {
            this.drawingService.updateDrawing(drawing);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "修改Drawing失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    @PostMapping("/drawing/excel")
    @RequiresPermissions("drawing:export")
    public void export(QueryRequest queryRequest, Drawing drawing, HttpServletResponse response) throws FebsException {
        try {
            List<Drawing> drawings = this.drawingService.findDrawings(queryRequest, drawing).getRecords();
            ExcelKit.$Export(Drawing.class, response).downXlsx(drawings, false);
        } catch (Exception e) {
            String message = "导出Excel失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    @GetMapping("/tree")
    public FebsResponse getDeptTree(Drawing drawing) throws FebsException {
        try {
            List<DrawlingTree<Drawing>> Drawings = this.drawingService.findDrawingsTrees(drawing);
            return new FebsResponse().success().data(Drawings);
        } catch (Exception e) {
            String message = "获取部门树失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

}
